package cn.com.cifi.mars.service.transfer.impl;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import cn.com.cifi.mars.common.exception.MsgException;
import cn.com.cifi.mars.constant.*;
import cn.com.cifi.mars.entity.*;
import cn.com.cifi.mars.mapper.mars.PriceVersionMapper;
import cn.com.cifi.mars.service.*;
import cn.com.cifi.mars.util.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;

import cn.com.cifi.mars.bean.bo.DictionaryCodeBO;
import cn.com.cifi.mars.bean.bo.MdmBuildAreaBO;
import cn.com.cifi.mars.bean.bo.MdmBuildAreaDetailBO;
import cn.com.cifi.mars.bean.bo.PanoPlanCastDecisionBO;
import cn.com.cifi.mars.bean.bo.PriceValueCastDecisionAllOrgEXTBO;
import cn.com.cifi.mars.entity.common.SysProjectFetchNumber;
import cn.com.cifi.mars.mapper.mars.PriceValueOrgMapper;
import cn.com.cifi.mars.mapper.mars.TransferResultMapper;
import cn.com.cifi.mars.service.impl.PriceVersionServiceImpl;
import cn.com.cifi.mars.service.impl.PriceVersionValueOrgServiceImpl;
import cn.com.cifi.mars.service.transfer.PriceVersionValueTransferService;
import cn.com.cifi.mars.service.transfer.TransferKingdeeValueService;
import cn.com.cifi.mars.service.transfer.TransferPriceVersionService;

@Service
public class PriceVersionValueTransferServiceImpl implements PriceVersionValueTransferService {


    @Autowired
    private TransferKingdeeValueService transferKingdeeValueService;

    @Autowired
    private PanoplanBuildPlanService panoplanBuildPlanService;

    @Autowired
    private BuildAreaService buildAreaService;

    @Autowired
    private PriceValueOrgService priceValueOrgService;

    @Autowired
    private StageService stageService;

    @Autowired
    private SysProjectFetchNumberService sysProjectFetchNumberService;

    @Autowired
    private ProjectPatnerService projectPatnerService;

    @Autowired
    private PriceVersionService priceVersionService;

    @Autowired
    private TransferPriceVersionService transferPriceVersionService;

    @Autowired
    private SysDictionaryService sysDictionaryService;

    @Autowired
    private PriceVersionValueOrgServiceImpl priceVersionValueOrgServiceImpl;

    @Autowired
    private PriceValueOrgMapper priceValueOrgMapper;

    @Autowired
    private PriceVersionServiceImpl priceVersionServiceImpl;

    @Autowired
    private TransferResultMapper transferResultMapper;

    @Autowired
    private PriceVersionMapper priceVersionMapper;

    @Autowired
    private StagePartNersService stagePartNersService;

    static List<String> isParkList = Arrays.asList("139","140","141","142","154");

    @Override
    public void priceVersionTransfer() {
        List<TransferResult> resultTransferResults = new ArrayList<TransferResult>();
        LocalDateTime now = LocalDateTime.now();

        //查询金蝶迁移数据表中失败或初始状态的项目信息
        QueryWrapper<TransferResult> tRQueryWrapper = new QueryWrapper<TransferResult>();
        tRQueryWrapper.in("states", "0","2");
        List<TransferResult> transferResults = transferResultMapper.selectList(tRQueryWrapper);
        if (CollectionUtils.isNotEmpty(transferResults)){
          zzwc : for (TransferResult transferResult : transferResults){
              try{
                  String stageCode = transferResult.getStageCode();
                  if (StringUtils.isNotEmpty(stageCode)){
                      List<TransferKingdeeValue> transferKingdeeValueList = transferKingdeeValueService.selectStageByStageCode(stageCode);
                      if (CollectionUtils.isNotEmpty(transferKingdeeValueList)){
                          zwc : for (TransferKingdeeValue transferKingdeeValue : transferKingdeeValueList){
                              String kingdeeValueStageId = transferKingdeeValue.getStageId();

                              //查询指定分期信息
                              MdmStage mdmStage = stageService.selectByKingdeeProjectfId(kingdeeValueStageId);
                              if (mdmStage == null){
                                  transferResult.setStates("2");
                                  transferResult.setRemark("【主数据】分期不存在");
                                  resultTransferResults.add(transferResult);

                                  continue zwc;
                              }
                              String stageId = mdmStage.getProjectFid();

                              String buildAreaStage = transferKingdeeValue.getBuildAreaStage();
                              String mdmVersionType = null;
                              String panoVersionType = null;
                              String versionType = null;

                              if (buildAreaStage.equals("房产预测版")){
                                  mdmVersionType = VersionTypeEunm.PRESURVEY.getValue();
                                  panoVersionType = VersionTypeEunm.DYNAMIC.getValue();
                                  versionType = VersionTypeEunm.DYNAMIC.getKey();
                              }else if (buildAreaStage.equals("房产实测版")){
                                  mdmVersionType = VersionTypeEunm.ACTUALSURVEY.getValue();
                                  panoVersionType = VersionTypeEunm.DYNAMIC.getValue();
                                  versionType = VersionTypeEunm.DYNAMIC.getKey();
                              }else if (buildAreaStage.equals("工规证版")){
                                  mdmVersionType = VersionTypeEunm.PERMIT.getValue();
                                  panoVersionType = VersionTypeEunm.DYNAMIC.getValue();
                                  versionType = VersionTypeEunm.DYNAMIC.getKey();
                              }else if (buildAreaStage.equals("战规版")){
                                  mdmVersionType = VersionTypeEunm.WARPLAN.getValue();
                                  panoVersionType = VersionTypeEunm.WARPLAN.getValue();
                                  versionType = VersionTypeEunm.WARPLAN.getKey();
                              }

                              if (buildAreaStage.equals("投资版") || buildAreaStage.equals("定位版")){
                                  transferResult.setStates("2");
                                  transferResult.setRemark("【金蝶】所处阶段为[投资版]或[定位版]");
                                  resultTransferResults.add(transferResult);

                                  //获取最新的投资版楼栋计划
                                  PanoPlanCastDecisionBO panoPlanCastDecisionBO = panoplanBuildPlanService.selectNewestByFid(stageId, Arrays.asList(VersionTypeEunm.INVESTMENT.getValue()),IsDeleteEnum.NO.getKey());
                                  if (panoPlanCastDecisionBO == null){

                                      transferResult.setStates("2");
                                      transferResult.setRemark("【楼栋计划】金蝶项目版本阶段对应楼栋计划版本不存在");
                                      resultTransferResults.add(transferResult);

                                      continue zwc;
                                  }

                                  //将节点为取预售证时间的存储在指定Map中
                                  Map<String, LocalDateTime> openTimeMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> openTimeActualMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> advanceSaleTimeMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> advanceSaleTimeActualMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> supplyTimeMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> supplyTimeActualMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> supplyTimeVirtualMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> openSaleTimeMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> openSaleTimeActualMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> cappingTimeMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> cappingTimeActualMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> completedTimeMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> completedTimeActualMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> carryOverTimeMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> carryOverTimeActualMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> actualDeliveryTimeMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> actualDeliveryTimeActualMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> contractDeliveryTimeMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> contractDeliveryTimeActualMap = new HashMap<String, LocalDateTime>();
                                  List<PanoplanBuildPlanItemDetail> details = panoPlanCastDecisionBO.getDetails();
                                  if(CollectionUtils.isNotEmpty(details)){
                                      for (int i = 0;i < details.size(); i++){
                                          PanoplanBuildPlanItemDetail panoplanBuildPlanItemDetail = details.get(i);
                                          String nodeCode = panoplanBuildPlanItemDetail.getNodeCode();
                                          if(StringUtils.isNotEmpty(nodeCode) && nodeCode.equals(NodeEnum.JT_LCB_00002.getKey())){
                                              openTimeMap.put(panoplanBuildPlanItemDetail.getPkId(), panoplanBuildPlanItemDetail.getPlannedFinishDate());
                                              openTimeActualMap.put(panoplanBuildPlanItemDetail.getPkId(), panoplanBuildPlanItemDetail.getActualFinishDate());
                                          }else if(StringUtils.isNotEmpty(nodeCode) && nodeCode.equals(NodeEnum.JT_YJ_00001.getKey())){
                                              advanceSaleTimeMap.put(panoplanBuildPlanItemDetail.getPkId(), panoplanBuildPlanItemDetail.getPlannedFinishDate());
                                              advanceSaleTimeActualMap.put(panoplanBuildPlanItemDetail.getPkId(), panoplanBuildPlanItemDetail.getActualFinishDate());
                                          }else if(StringUtils.isNotEmpty(nodeCode) && nodeCode.equals(NodeEnum.JT_YJ_00002.getKey())){
                                              supplyTimeMap.put(panoplanBuildPlanItemDetail.getPkId(), panoplanBuildPlanItemDetail.getPlannedFinishDate());
                                              supplyTimeActualMap.put(panoplanBuildPlanItemDetail.getPkId(), panoplanBuildPlanItemDetail.getActualFinishDate());
                                              supplyTimeVirtualMap.put(panoplanBuildPlanItemDetail.getPkId(), panoplanBuildPlanItemDetail.getVirtualFinishDate());
                                          }else if(StringUtils.isNotEmpty(nodeCode) && nodeCode.equals(NodeEnum.JT_LCB_00003.getKey())){
                                              openSaleTimeMap.put(panoplanBuildPlanItemDetail.getPkId(), panoplanBuildPlanItemDetail.getPlannedFinishDate());
                                              openSaleTimeActualMap.put(panoplanBuildPlanItemDetail.getPkId(), panoplanBuildPlanItemDetail.getActualFinishDate());
                                          }else if(StringUtils.isNotEmpty(nodeCode) && nodeCode.equals(NodeEnum.JT_YJ_00003.getKey())){
                                              cappingTimeMap.put(panoplanBuildPlanItemDetail.getPkId(), panoplanBuildPlanItemDetail.getPlannedFinishDate());
                                              cappingTimeActualMap.put(panoplanBuildPlanItemDetail.getPkId(), panoplanBuildPlanItemDetail.getActualFinishDate());
                                          }else if(StringUtils.isNotEmpty(nodeCode) && nodeCode.equals(NodeEnum.JT_LCB_00004.getKey())){
                                              completedTimeMap.put(panoplanBuildPlanItemDetail.getPkId(), panoplanBuildPlanItemDetail.getPlannedFinishDate());
                                              completedTimeActualMap.put(panoplanBuildPlanItemDetail.getPkId(), panoplanBuildPlanItemDetail.getActualFinishDate());
                                          }else if(StringUtils.isNotEmpty(nodeCode) && nodeCode.equals(NodeEnum.JT_LCB_00006.getKey())){
                                              carryOverTimeMap.put(panoplanBuildPlanItemDetail.getPkId(), panoplanBuildPlanItemDetail.getPlannedFinishDate());
                                              carryOverTimeActualMap.put(panoplanBuildPlanItemDetail.getPkId(), panoplanBuildPlanItemDetail.getActualFinishDate());
                                          }else if(StringUtils.isNotEmpty(nodeCode) && nodeCode.equals(NodeEnum.JT_LCB_00005.getKey())){
                                              actualDeliveryTimeMap.put(panoplanBuildPlanItemDetail.getPkId(), panoplanBuildPlanItemDetail.getPlannedFinishDate());
                                              actualDeliveryTimeActualMap.put(panoplanBuildPlanItemDetail.getPkId(), panoplanBuildPlanItemDetail.getActualFinishDate());
                                          }else if(StringUtils.isNotEmpty(nodeCode) && nodeCode.equals(NodeEnum.NODE_000.getKey())){
                                              contractDeliveryTimeMap.put(panoplanBuildPlanItemDetail.getPkId(), panoplanBuildPlanItemDetail.getPlannedFinishDate());
                                              contractDeliveryTimeActualMap.put(panoplanBuildPlanItemDetail.getPkId(), panoplanBuildPlanItemDetail.getActualFinishDate());
                                          }
                                      }
                                  }

                                  //分期有可售或阶段性自持产品构成没有批次或供货日期时，不允许新建该分期投资版价格版本
                                  List<PanoplanBuildPlanItem> productItems = panoPlanCastDecisionBO.getProductItems();
                                  for (int i = 0;i < productItems.size(); i++){
                                      PanoplanBuildPlanItem panoplanBuildPlanItem = productItems.get(i);
                                      String saleNum = panoplanBuildPlanItem.getSaleNum();
                                      if(StringUtils.isNotEmpty(saleNum) && (saleNum.equals(SalyTypeEnum.SALABLE.getKey())
                                              || saleNum.equals(SalyTypeEnum.STAGE_SELF_SUSTAINMENT.getKey()))){
                                          LocalDateTime supplyTime = supplyTimeMap.get(panoplanBuildPlanItem.getId());
                                          LocalDateTime supplyActualTime = supplyTimeActualMap.get(panoplanBuildPlanItem.getId());
                                          LocalDateTime supplyVirtualTime = supplyTimeVirtualMap.get(panoplanBuildPlanItem.getId());
                                          if(supplyTime == null && supplyActualTime == null && supplyVirtualTime == null){

                                              transferResult.setStates("2");
                                              transferResult.setRemark("【楼栋计划】分期有可售或阶段性自持产品构成没有批次或供货日期时，不允许新建该分期价格版本");
                                              resultTransferResults.add(transferResult);

                                              continue zwc;
                                          }
                                      }
                                  }

                                  //查询最新审批通过的价格管理投资版
                                  String oldVersionId = "";
                                  String oldVersionNum = "";
                                  Map<String, PriceValueCastDecisionAllOrgEXTBO> preProductMap = new HashMap<String, PriceValueCastDecisionAllOrgEXTBO>();
                                  Map<String, PriceValueCastDecisionAllOrgEXTBO> preParkingMap = new HashMap<String, PriceValueCastDecisionAllOrgEXTBO>();
                                  Map<String, PriceValueCastDecisionAllOrgEXTBO> preNotSaleParkingMap = new HashMap<String, PriceValueCastDecisionAllOrgEXTBO>();
                                  PriceVersion oldPriceVersion = priceVersionMapper.selectNewestVersionByFid(stageId, VersionTypeEunm.INVESTMENT.getKey(), Arrays.asList(VersionStatusEnum.SYSGENERATION.getKey(), VersionStatusEnum.SUCCESSAPPROVED.getKey()), IsDeleteEnum.NO.getKey());
                                  if (oldPriceVersion != null){
                                      oldVersionId = oldPriceVersion.getId();
                                      oldVersionNum = oldPriceVersion.getVersionNum();

                                      //获取上版本产品构成以及可售车位和待售车位数据
                                      List<PriceValueCastDecisionAllOrgEXTBO> prePriceValueOrgList = priceValueOrgMapper.selectCastDecisionAllOrg(oldVersionId, IsDeleteEnum.NO.getKey());
                                      for (PriceValueCastDecisionAllOrgEXTBO preOrg : prePriceValueOrgList){
                                          String type = preOrg.getType();
                                          if(type.equals(OrgTypeEunm.ORG_SALING_PARKING.getKey())){
                                              preParkingMap.put(preOrg.getCode(), preOrg);
                                          }else if (type.equals(OrgTypeEunm.ORG_NOT_SALING_PARKING.getKey())){
                                              preNotSaleParkingMap.put(preOrg.getCode(), preOrg);
                                          } else{
                                              preProductMap.put(preOrg.getCode(), preOrg);
                                          }
                                      }
                                  }

                                  //查询是否营销操盘
                                  //项目操盘方式
                                  //DULICP=独立操盘-------营销操盘
                                  //LIANHECP=联合操盘-------非营销操盘
                                  //BUCP=不操盘-------非营销操盘
                                  String market = null;
                                  //MdmProjectPatner partner = projectPatnerService.selectByFidAndPartnerNo(mdmStage.getProjectId());
                                  StagePartNers partner = stagePartNersService.getStagePartnersByProjectFid(mdmStage.getProjectFid());
                                  if(partner != null){
                                      String isOpMarketing = partner.getIsOpMarketing();
                                      if(StringUtils.isNotEmpty(isOpMarketing) && (MarketingEunm.LIANHECP.getValue().equals(isOpMarketing) || MarketingEunm.BUCP.getValue().equals(isOpMarketing))){
                                          market = MarketingEunm.IS_NOT_MARKET.getKey();
                                      }else if (StringUtils.isNotEmpty(isOpMarketing) && MarketingEunm.DULICP.getValue().equals(isOpMarketing)){
                                          market = MarketingEunm.IS_MARKET.getKey();
                                      }
                                  }else{
                                      throw new MsgException(CodeEnum.NON_MARKET_ERROR);
                                  }

                                  //创建新版本
                                  String newVersionId = UUIDUtils.create();
                                  PriceVersion newVersion = new PriceVersion();
                                  String newVersionNum = "";
                                  if(oldPriceVersion != null){
                                      oldVersionId = oldPriceVersion.getId();
                                      newVersionNum = VersionUtils.bigVersionInc(oldPriceVersion.getVersionNum());
                                      newVersion.setPreVersionId(oldPriceVersion.getId());
                                      newVersion.setPreVersionNum(oldPriceVersion.getVersionNum());
                                      newVersion.setIsAssessmentVersion(IsDeleteEnum.NO.getKey());
                                  }else{//第一次创建版本
                                      newVersionNum = VersionUtils.bigVersionInc("");
                                      newVersion.setPreVersionId("");
                                      newVersion.setPreVersionNum("");
                                      newVersion.setIsAssessmentVersion(IsDeleteEnum.YES.getKey());
                                  }

                                  newVersion.setPreVersionId(oldVersionId);
                                  newVersion.setPreVersionNum(oldVersionNum);
                                  newVersion.setId(newVersionId);
                                  newVersion.setProjectId(mdmStage.getProjectId());
                                  newVersion.setProjectfId(stageId);
                                  newVersion.setStageName(mdmStage.getStageName());
                                  newVersion.setStageCode(mdmStage.getStageCode());
                                  newVersion.setVersionName(VersionTypeEunm.INVESTMENT.getValue()+newVersionNum);
                                  newVersion.setVersionNum(newVersionNum);
                                  newVersion.setVersionType(VersionTypeEunm.INVESTMENT.getKey());
                                  newVersion.setVersionCreateState(VersionCreateEnum.MANUALCREATE.getKey());
                                  newVersion.setCreateBy(Constants.SYS);
                                  newVersion.setVersionState(VersionStatusEnum.NOTPREPARED.getKey());
                                  newVersion.setRemake("");
                                  newVersion.setSourcePlan(panoPlanCastDecisionBO.getPanoplanBuildPlan().getId());
                                  newVersion.setSourcePlanDetail("["+DateUtils.dateToString(LocalDateTimeUtils.convertLDTToDate(panoPlanCastDecisionBO.getPanoplanBuildPlan().getCreatedDate()) ,
                                          DateUtils.DATE_KEY_SMALL_STR) + "]-[" +  panoPlanCastDecisionBO.getPanoplanBuildPlan().getVersionType() +"]-[" +
                                          panoPlanCastDecisionBO.getPanoplanBuildPlan().getPlanVersion()+"]");
                                  newVersion.setSourceValue(oldVersionId);
                                  newVersion.setSourcePanoVersionType(panoPlanCastDecisionBO.getPanoplanBuildPlan().getVersionType());
                                  newVersion.setSourcePanoPlanVersion(panoPlanCastDecisionBO.getPanoplanBuildPlan().getPlanVersion());
                                  newVersion.setSourcePanoCreatedDate(panoPlanCastDecisionBO.getPanoplanBuildPlan().getCreatedDate());
                                  newVersion.setIsMarket(market);
                                  newVersion.setIsDelete(IsDeleteEnum.NO.getKey());
                                  newVersion.setCreateTime(now);
                                  newVersion.setUpdateTime(now);

                                  //创建分期
                                  PriceValueOrg projectOrg = new PriceValueOrg();
                                  String projectOrgId = UUIDUtils.create();
                                  projectOrg.setId(projectOrgId);
                                  projectOrg.setVersionId(newVersionId);
                                  projectOrg.setPid("0");
                                  projectOrg.setType(OrgTypeEunm.ORG_STAGE.getKey());
                                  projectOrg.setOrgId(mdmStage.getProjectFid());
                                  projectOrg.setOrgCode(mdmStage.getStageCode());
                                  projectOrg.setOrgName(mdmStage.getStageName());
                                  projectOrg.setIsDelete(IsDeleteEnum.NO.getKey());
                                  projectOrg.setCreateTime(now);
                                  projectOrg.setUpdateTime(now);

                                  //创建组团
                                  Map<String, PriceValueOrg> groupInsertMap = new HashMap<String, PriceValueOrg>();
                                  Map<String, PriceValueOrg> groupMap = new HashMap<String, PriceValueOrg>();
                                  List<PanoplanBuildPlanItem> groupItems = panoPlanCastDecisionBO.getGroupItems();
                                  for(PanoplanBuildPlanItem groupItem : groupItems){
                                      PriceValueOrg priceValueOrg = new PriceValueOrg();
                                      String groupId = UUIDUtils.create();
                                      priceValueOrg.setId(groupId);
                                      priceValueOrg.setVersionId(newVersionId);
                                      priceValueOrg.setPid(projectOrgId);
                                      priceValueOrg.setType(OrgTypeEunm.ORG_GROUP.getKey());
                                      priceValueOrg.setOrgId(groupItem.getBatchId());
                                      priceValueOrg.setOrgCode(groupItem.getBatchCode());
                                      priceValueOrg.setOrgName(groupItem.getBatchName());
                                      priceValueOrg.setIsDelete(IsDeleteEnum.NO.getKey());
                                      priceValueOrg.setCreateTime(now);
                                      priceValueOrg.setUpdateTime(now);

                                      groupInsertMap.put(groupId, priceValueOrg);
                                      groupMap.put(groupItem.getBatchId() ,priceValueOrg);
                                  }

                                  //创建批次
                                  Map<String, PriceValueOrg> batchInsertMap = new HashMap<String, PriceValueOrg>();
                                  Map<String, String> batchMap = new HashMap<String, String>();
                                  List<PanoplanBuildPlanItem> batchItems = panoPlanCastDecisionBO.getBatchItems();
                                  for(PanoplanBuildPlanItem batchItem : batchItems){
                                      PriceValueOrg priceValueOrg = new PriceValueOrg();
                                      String batchId = UUIDUtils.create();
                                      priceValueOrg.setId(batchId);
                                      priceValueOrg.setVersionId(newVersionId);
                                      priceValueOrg.setPid(groupMap.get(batchItem.getBatchId()).getId());
                                      priceValueOrg.setType(OrgTypeEunm.ORG_BATCH.getKey());
                                      priceValueOrg.setOrgId("");
                                      priceValueOrg.setOrgCode(batchItem.getBatchNum());
                                      priceValueOrg.setOrgName(NumberEnum.getValueByKey(batchItem.getBatchNum().trim())+"批次");
                                      priceValueOrg.setIsDelete(IsDeleteEnum.NO.getKey());
                                      priceValueOrg.setCreateTime(now);
                                      priceValueOrg.setUpdateTime(now);

                                      batchInsertMap.put(batchId, priceValueOrg);
                                      batchMap.put(batchItem.getBatchId()+"&"+batchItem.getBatchNum() ,batchId);
                                  }

                                  //构建product
                                  List<PanoplanBuildPlanItem> pproductItems = panoPlanCastDecisionBO.getProductItems();

                                  //找上级分类
                                  Map<String, DictionaryCodeBO> dicMap = new HashMap();
                                  List<String> productCodes = Optional.of(pproductItems).orElseGet(ArrayList::new)
                                          .stream().map(PanoplanBuildPlanItem::getProductTypeNum)
                                          .collect(Collectors.toList());
                                  if(CollectionUtils.isNotEmpty(productCodes)){
                                      List<DictionaryCodeBO> dicType = sysDictionaryService.selectByCodes(productCodes);
                                      if(CollectionUtils.isNotEmpty(dicType)){
                                          for(DictionaryCodeBO bo : dicType){
                                              dicMap.put(bo.getChildCode(),bo);
                                          }
                                      }
                                  }

                                  //归集金蝶产品构成
                                  Map<String, TransferKingdeeValue> jdMap = jdCollectProduct(mdmStage.getStageCode());

                                  //创建产品构成
                                  List<PriceValueOrg> productList = new ArrayList<PriceValueOrg>();
                                  for(PanoplanBuildPlanItem productItem : pproductItems){
                                      PriceValueOrg priceValueOrg = new PriceValueOrg();

                                      String productCode = productItem.getProductTypeNum()+"."+productItem.getDecorateNum()+"."+productItem.getSaleNum()+"."+productItem.getProTypeCode();

                                      //赋值基本信息
                                      String productId = UUIDUtils.create();
                                      priceValueOrg.setId(productId);
                                      priceValueOrg.setVersionId(newVersionId);
                                      priceValueOrg.setPid(batchMap.get(productItem.getBatchId()+"&"+productItem.getBatchNum()));
                                      priceValueOrg.setType(OrgTypeEunm.ORG_PRODUCT.getKey());
                                      priceValueOrg.setOrgId("");
                                      priceValueOrg.setOrgCode(productItem.getProductTypeNum());
                                      priceValueOrg.setOrgName(productItem.getProductTypeName());

                                      //赋值楼栋计划信息
                                      priceValueOrg.setPanoBatchSaleArea(productItem.getTotalSaleArea());
                                      priceValueOrg.setPanoTotalRoomNum(productItem.getTotalRoomNum());
                                      priceValueOrg.setPanoTotalSaleArea(productItem.getTotalSaleArea());
                                      priceValueOrg.setPanoBatchRoomNum(productItem.getBatchRoomNum());
                                      priceValueOrg.setPanoBatchSaleArea(productItem.getBatchSaleArea());
                                      priceValueOrg.setPanoSalePark(productItem.getSalePark());
                                      priceValueOrg.setPanoTotalBuildArea(productItem.getTotalBuildArea());
                                      priceValueOrg.setPanoBatchBuildArea(productItem.getBatchBuildArea());

                                      //赋值属性标签信息
                                      if(StringUtils.isNotEmpty(productItem.getSaleNum()) && StringUtils.isNotEmpty(productItem.getSaleType())){
                                          priceValueOrg.setBusinessType(productItem.getSaleNum());
                                          priceValueOrg.setBusinessTypeName(productItem.getSaleType());
                                      }else{
                                          transferResult.setStates("2");
                                          transferResult.setRemark(CodeEnum.NON_EXISTENT_BUSINESSTYPE.getMessage());
                                          resultTransferResults.add(transferResult);

                                          continue zwc;
                                      }
                                      if(StringUtils.isNotEmpty(productItem.getDecorateNum()) && StringUtils.isNotEmpty(productItem.getDecorateName())){
                                          priceValueOrg.setFreeType(productItem.getDecorateNum());
                                          priceValueOrg.setFreeTypeName(productItem.getDecorateName());
                                      }else{
                                          transferResult.setStates("2");
                                          transferResult.setRemark(CodeEnum.NON_EXISTENT_FREETYPE.getMessage());
                                          resultTransferResults.add(transferResult);

                                          continue zwc;
                                      }
                                      if(StringUtils.isNotEmpty(productItem.getProTypeCode()) && StringUtils.isNotEmpty(productItem.getProType())){
                                          priceValueOrg.setProType(productItem.getProTypeCode());
                                          priceValueOrg.setProTypeName(productItem.getProType());
                                      }else{
                                          transferResult.setStates("2");
                                          transferResult.setRemark(CodeEnum.NON_EXISTENT_PROTYPE.getMessage());
                                          resultTransferResults.add(transferResult);

                                          continue zwc;
                                      }
                                      priceValueOrg.setProductCode(productItem.getProductTypeNum());
                                      priceValueOrg.setProductType(productItem.getProductTypeName());
                                      priceValueOrg.setProProductCode(dicMap.get(productItem.getProductTypeNum()).getParentCode());
                                      priceValueOrg.setProProductType(dicMap.get(productItem.getProductTypeNum()).getParentName());

                                      //匹配时间节点
                                      priceValueOrg.setOpenTime(openTimeMap.get(productItem.getId()));
                                      priceValueOrg.setOpenTimeActual(openTimeActualMap.get(productItem.getId()));
                                      priceValueOrg.setAdvanceSaleTime(advanceSaleTimeMap.get(productItem.getId()));
                                      priceValueOrg.setAdvanceSaleTimeActual(advanceSaleTimeActualMap.get(productItem.getId()));
                                      priceValueOrg.setSupplyTime(supplyTimeMap.get(productItem.getId()));
                                      priceValueOrg.setSupplyTimeActual(supplyTimeActualMap.get(productItem.getId()));
                                      priceValueOrg.setSupplyTimeVirtual(supplyTimeVirtualMap.get(productItem.getId()));
                                      priceValueOrg.setOpenSaleTime(openSaleTimeMap.get(productItem.getId()));
                                      priceValueOrg.setOpenSaleTimeActual(openSaleTimeActualMap.get(productItem.getId()));
                                      priceValueOrg.setCappingTime(cappingTimeMap.get(productItem.getId()));
                                      priceValueOrg.setCappingTimeActual(cappingTimeActualMap.get(productItem.getId()));
                                      priceValueOrg.setCompletedTime(completedTimeMap.get(productItem.getId()));
                                      priceValueOrg.setCompletedTimeActual(completedTimeActualMap.get(productItem.getId()));
                                      priceValueOrg.setCarryOverTime(carryOverTimeMap.get(productItem.getId()));
                                      priceValueOrg.setCarryOverTimeActual(carryOverTimeActualMap.get(productItem.getId()));
                                      priceValueOrg.setActualDeliveryTime(actualDeliveryTimeMap.get(productItem.getId()));
                                      priceValueOrg.setActualDeliveryTimeActual(actualDeliveryTimeActualMap.get(productItem.getId()));
                                      priceValueOrg.setContractDeliveryTime(contractDeliveryTimeMap.get(productItem.getId()));
                                      priceValueOrg.setContractDeliveryTimeActual(contractDeliveryTimeActualMap.get(productItem.getId()));

                                      //赋值货值信息
                                      priceValueOrg.setNum(productItem.getBatchRoomNum());
                                      if(isParkList.contains(productItem.getProductTypeNum())){
                                          priceValueOrg.setIsParking(IsDeleteEnum.YES.getKey());
                                          priceValueOrg.setParkingNum(productItem.getBatchRoomNum());
                                      }else{
                                          priceValueOrg.setIsParking(IsDeleteEnum.NO.getKey());
                                          priceValueOrg.setRoomNum(productItem.getBatchRoomNum());
                                      }
                                      priceValueOrg.setIsSplit(IsDeleteEnum.NO.getKey());
                                      priceValueOrg.setIsDelete(IsDeleteEnum.NO.getKey());
                                      priceValueOrg.setCreateTime(now);
                                      priceValueOrg.setUpdateTime(now);

                                      Integer batchRoomNum = productItem.getBatchRoomNum();
                                      priceValueOrg.setNum(batchRoomNum);

                                      BigDecimal saleArea = productItem.getBatchSaleArea() == null ? new BigDecimal(0) : productItem.getBatchSaleArea();
                                      priceValueOrg.setSaleArea(saleArea);

                                      if(productItem.getSaleNum().equals(SalyTypeEnum.SALABLE.getKey())
                                              || productItem.getSaleNum().equals(SalyTypeEnum.STAGE_SELF_SUSTAINMENT.getKey())){
                                          if (batchRoomNum <= 0){
                                              transferResult.setStates("2");
                                              transferResult.setRemark("产品构成信息不完整:"+productItem.getProductTypeName()+"套数/个数小于等于0");
                                              resultTransferResults.add(transferResult);

                                              continue zwc;
                                          }

                                          if (saleArea.compareTo(new BigDecimal(0)) <= 0){
                                              if (!isParkList.contains(productItem.getProductTypeNum())){
                                                  transferResult.setStates("2");
                                                  transferResult.setRemark("产品构成信息不完整:"+productItem.getProductTypeName()+"面积小于等于0");
                                                  resultTransferResults.add(transferResult);

                                                  continue zwc;
                                              }
                                          }
                                      }

                                      String batchNum = productItem.getBatchNum();
                                      TransferKingdeeValue tkValue = jdMap.get(batchNum+"&"+productCode);
                                      if (tkValue != null){
                                          BigDecimal transferAvgPrice = tkValue.getAvgPrice();
                                          if (isParkList.contains(productItem.getProductTypeNum())){
                                              priceValueOrg.setAveragePriceParking(transferAvgPrice);
                                              priceValueOrg.setNotCommodityValue(priceVersionValueOrgServiceImpl.BigDecimalMUL(transferAvgPrice, new BigDecimal(priceValueOrg.getParkingNum())));
                                          }else{
                                              priceValueOrg.setAveragePriceHouse(transferAvgPrice);
                                              priceValueOrg.setHouseCommodityValue(priceVersionValueOrgServiceImpl.BigDecimalMUL(transferAvgPrice, priceValueOrg.getSaleArea()));
                                          }
                                      }

                                      productList.add(priceValueOrg);
                                  }

                                  //货值汇总
                                  List<PriceValueOrg> productInsertList = new ArrayList<PriceValueOrg>();
                                  for (PriceValueOrg productOrg : productList){

                                      String businessType = productOrg.getBusinessType();
                                      if (businessType.equals(SalyTypeEnum.NOT_RENTABLE.getKey()) || businessType.equals(SalyTypeEnum.RENTED.getKey())){
                                          productInsertList.add(productOrg);
                                          continue;
                                      }

                                      PriceValueOrg batchOrg = batchInsertMap.get(productOrg.getPid());
                                      priceVersionValueOrgServiceImpl.valueUpSummary(batchOrg, productOrg);
                                      batchInsertMap.put(productOrg.getPid(), batchOrg);

                                      productInsertList.add(productOrg);
                                  }

                                  List<PriceValueOrg> batchInsertList = new ArrayList<PriceValueOrg>();
                                  if (CollectionUtils.isNotEmpty(batchInsertMap)){
                                      for (Map.Entry<String, PriceValueOrg> batchOrgMap : batchInsertMap.entrySet()){

                                          PriceValueOrg batchOrg = batchOrgMap.getValue();
                                          PriceValueOrg groupOrg = groupInsertMap.get(batchOrg.getPid());
                                          priceVersionValueOrgServiceImpl.valueUpSummary(groupOrg, batchOrg);
                                          groupInsertMap.put(batchOrg.getPid(), groupOrg);

                                          batchInsertList.add(batchOrg);
                                      }
                                  }

                                  List<PriceValueOrg> groupInsertList = new ArrayList<PriceValueOrg>();
                                  if (CollectionUtils.isNotEmpty(groupInsertMap)){
                                      for (Map.Entry<String, PriceValueOrg> groupOrgMap : groupInsertMap.entrySet()){

                                          PriceValueOrg groupOrg = groupOrgMap.getValue();
                                          priceVersionValueOrgServiceImpl.valueUpSummary(projectOrg, groupOrg);

                                          groupInsertList.add(groupOrg);
                                      }
                                  }

                                  //测试保存
                                  try{
                                      priceVersionService.saveInfo(newVersion,Arrays.asList(projectOrg),groupInsertList,productInsertList,batchInsertList,null,null);
                                      transferPriceVersionService.saveInfo(newVersion,Arrays.asList(projectOrg),groupInsertList,productInsertList,batchInsertList,null,null);

                                      transferResult.setStates("1");
                                      transferResult.setRemark("成功");
                                      transferResult.setPriceVersionId(newVersionId);
                                      resultTransferResults.add(transferResult);
                                  }catch (Exception e){
                                      transferResult.setStates("2");
                                      transferResult.setRemark("【异常】保存货值版本出错:"+e.getMessage());
                                      resultTransferResults.add(transferResult);
                                  }

                                  continue zwc;
                              }else{

                                  //获取最新的战规版楼栋计划
                                  PanoPlanCastDecisionBO panoPlanCastDecisionBO = panoplanBuildPlanService.selectNewestWarPlanByFidAndVersionType(stageId, panoVersionType, IsDeleteEnum.NO.getKey());
                                  if (panoPlanCastDecisionBO == null){

                                      transferResult.setStates("2");
                                      transferResult.setRemark("【楼栋计划】金蝶项目版本阶段对应楼栋计划版本不存在");
                                      resultTransferResults.add(transferResult);

                                      continue zwc;
                                  }

                                  //获取最新的战规版楼栋面积
                                  MdmBuildAreaBO mdmProductWarPlanBO = buildAreaService.selectNewestByFidAndVersionType(stageId, mdmVersionType, IsDeleteEnum.NO.getKey());
                                  if(mdmProductWarPlanBO == null){

                                      transferResult.setStates("2");
                                      transferResult.setRemark("【主数据】金蝶项目版本阶段对应主数据版本不存在");
                                      resultTransferResults.add(transferResult);

                                      continue zwc;
                                  }

                                  //全景计划系统最新版楼栋计划项目树与主数据系统最新版面积版本项目树不匹配，不允许创建新版本；
                                  if(panoPlanCastDecisionBO.getGroupItems().size() == mdmProductWarPlanBO.getGroupItems().size()){
                                      List<String> groupMap = Optional.of(panoPlanCastDecisionBO.getGroupItems()).orElseGet(ArrayList::new)
                                              .stream().map(PanoplanBuildPlanItem::getBatchCode)
                                              .collect(Collectors.toList());
                                      for(MdmBuildAreaDetailBO buildAreaBO : mdmProductWarPlanBO.getGroupItems()){
                                          String groupCode = buildAreaBO.getGroupCode();
                                          if(!groupMap.contains(groupCode)){

                                              transferResult.setStates("2");
                                              transferResult.setRemark("【主数据|楼栋计划】主数据和楼栋计划组团不一致");
                                              resultTransferResults.add(transferResult);

                                              continue zwc;
                                          }else{
                                              groupMap.remove(groupCode);
                                          }
                                      }
                                      if(CollectionUtils.isNotEmpty(groupMap)){

                                          transferResult.setStates("2");
                                          transferResult.setRemark("【主数据|楼栋计划】主数据和楼栋计划组团不一致");
                                          resultTransferResults.add(transferResult);
                                          continue zwc;
                                      }
                                  }else{

                                      transferResult.setStates("2");
                                      transferResult.setRemark("【主数据|楼栋计划】主数据和楼栋计划组团不一致");
                                      resultTransferResults.add(transferResult);

                                      continue zwc;
                                  }

                                  if(panoPlanCastDecisionBO.getProductItems().size() == mdmProductWarPlanBO.getProductItems().size()){
                                      List<String> productList = new ArrayList<String>();
                                      for(PanoplanBuildPlanItem productItem : panoPlanCastDecisionBO.getProductItems()){
                                          productList.add(productItem.getBatchCode() + "&" + productItem.getProductTypeNum());
                                      }
                                      for(MdmBuildAreaDetailBO buildAreaBO : mdmProductWarPlanBO.getProductItems()){
                                          if(!productList.contains(buildAreaBO.getGroupCode() +"&"+ buildAreaBO.getProductCode())){

                                              transferResult.setStates("2");
                                              transferResult.setRemark("【主数据|楼栋计划】主数据和楼栋计划产品构成不一致");
                                              resultTransferResults.add(transferResult);

                                              continue zwc;
                                          }else{
                                              productList.remove(buildAreaBO.getGroupCode()+"&"+buildAreaBO.getProductCode());
                                          }
                                      }
                                      if(CollectionUtils.isNotEmpty(productList)){

                                          transferResult.setStates("2");
                                          transferResult.setRemark("【主数据|楼栋计划】主数据和楼栋计划产品构成不一致");
                                          resultTransferResults.add(transferResult);

                                          continue zwc;
                                      }
                                  }else{

                                      transferResult.setStates("2");
                                      transferResult.setRemark("【主数据|楼栋计划】主数据和楼栋计划产品构成不一致");
                                      resultTransferResults.add(transferResult);

                                      continue zwc;
                                  }

                                  Map<String, String> panoplanBuildPlanItemMap = new HashMap();
                                  Map<String, PanoplanBuildPlanItem> panoplanItemMap = new HashMap();
                                  if(panoPlanCastDecisionBO.getBuildItems().size() == mdmProductWarPlanBO.getBuildItems().size()){
                                      List<String> buildList = new ArrayList<String>();
                                      for(PanoplanBuildPlanItem buildItem : panoPlanCastDecisionBO.getBuildItems()){
                                          buildList.add(buildItem.getBatchId() + "&" + buildItem.getProductTypeNum() + "&" + buildItem.getBldPrdId());
                                          panoplanBuildPlanItemMap.put(buildItem.getId(), buildItem.getBatchId() + "&" + buildItem.getProductTypeNum() + "&" + buildItem.getBldPrdId());
                                          panoplanItemMap.put(buildItem.getBatchId() + "&" + buildItem.getProductTypeNum() + "&" + buildItem.getBldPrdId(), buildItem);
                                      }

                                      for(MdmBuildAreaDetailBO buildAreaBO : mdmProductWarPlanBO.getBuildItems()){
                                          if(buildList.contains(buildAreaBO.getGroupId()+"&"+buildAreaBO.getProductCode()+"&"+buildAreaBO.getBldPrdId())){

                                              if (buildAreaBO.getBusinessType().equals(SalyTypeEnum.SALABLE.getKey())
                                                      || buildAreaBO.getBusinessType().equals(SalyTypeEnum.STAGE_SELF_SUSTAINMENT.getKey())){
                                                  BigDecimal totalSaleArea = buildAreaBO.getTotalSaleArea()==null?new BigDecimal(0):buildAreaBO.getTotalSaleArea();
                                                  if (totalSaleArea.compareTo(new BigDecimal(0)) <= 0 ){
                                                      if (!isParkList.contains(buildAreaBO.getProductCode())){
                                                          String message = "楼栋信息不完整:"+buildAreaBO.getFormatBuildingName()+"面积小于等于0";
                                                          CodeEnum.NON_PANOPLAN_BUILD.setMessage(message);

                                                          transferResult.setStates("2");
                                                          transferResult.setRemark("【主数据|楼栋计划】主数据和楼栋计划楼栋不一致");
                                                          resultTransferResults.add(transferResult);

                                                          continue zwc;
                                                      }
                                                  }

                                                  Integer roomNum = buildAreaBO.getRoomNum()==null?0:buildAreaBO.getRoomNum();
                                                  if (roomNum <= 0){
                                                      String message = "楼栋信息不完整:"+buildAreaBO.getFormatBuildingName()+"套数/个数小于等于0";
                                                      CodeEnum.NON_PANOPLAN_BUILD.setMessage(message);

                                                      transferResult.setStates("2");
                                                      transferResult.setRemark("【主数据|楼栋计划】主数据和楼栋计划楼栋不一致");
                                                      resultTransferResults.add(transferResult);

                                                      continue zwc;
                                                  }
                                              }

                                              buildList.remove(buildAreaBO.getGroupId()+"&"+buildAreaBO.getProductCode()+"&"+buildAreaBO.getBldPrdId());
                                          }else{

                                              transferResult.setStates("2");
                                              transferResult.setRemark("【主数据|楼栋计划】主数据和楼栋计划楼栋不一致");
                                              resultTransferResults.add(transferResult);

                                              continue zwc;
                                          }
                                      }

                                      if(CollectionUtils.isNotEmpty(buildList)){

                                          transferResult.setStates("2");
                                          transferResult.setRemark("【主数据|楼栋计划】主数据和楼栋计划楼栋不一致");
                                          resultTransferResults.add(transferResult);

                                          continue zwc;
                                      }
                                  }else{

                                      transferResult.setStates("2");
                                      transferResult.setRemark("【主数据|楼栋计划】主数据和楼栋计划楼栋不一致");
                                      resultTransferResults.add(transferResult);

                                      continue zwc;
                                  }

                                  //楼栋计划匹配节点
                                  Map<String, LocalDateTime> openTimeMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> openTimeActualMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> advanceSaleTimeMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> advanceSaleTimeActualMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> supplyTimeMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> supplyTimeActualMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> supplyTimeVirtuallMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> openSaleTimeMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> openSaleTimeActualMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> cappingTimeMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> cappingTimeActualMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> completedTimeMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> completedTimeActualMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> carryOverTimeMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> carryOverTimeActualMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> actualDeliveryTimeMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> actualDeliveryTimeActualMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> contractDeliveryTimeMap = new HashMap<String, LocalDateTime>();
                                  Map<String, LocalDateTime> contractDeliveryTimeActualMap = new HashMap<String, LocalDateTime>();
                                  if(CollectionUtils.isNotEmpty(panoPlanCastDecisionBO.getDetails())){
                                      for(PanoplanBuildPlanItemDetail buildDetailItem : panoPlanCastDecisionBO.getDetails()){
                                          String codes = panoplanBuildPlanItemMap.get(buildDetailItem.getPkId());
                                          if(StringUtils.isNotEmpty(codes)){
                                              String nodeCode = buildDetailItem.getNodeCode();
                                              if(StringUtils.isNotEmpty(nodeCode) && nodeCode.equals(NodeEnum.JT_LCB_00002.getKey())){
                                                  openTimeMap.put(codes, buildDetailItem.getPlannedFinishDate());
                                                  openTimeActualMap.put(codes, buildDetailItem.getActualFinishDate());
                                              }else if(StringUtils.isNotEmpty(nodeCode) && nodeCode.equals(NodeEnum.JT_YJ_00001.getKey())){
                                                  advanceSaleTimeMap.put(codes, buildDetailItem.getPlannedFinishDate());
                                                  advanceSaleTimeActualMap.put(codes, buildDetailItem.getActualFinishDate());
                                              }else if(StringUtils.isNotEmpty(nodeCode) && nodeCode.equals(NodeEnum.JT_YJ_00002.getKey())){
                                                  supplyTimeMap.put(codes, buildDetailItem.getPlannedFinishDate());
                                                  supplyTimeActualMap.put(codes, buildDetailItem.getActualFinishDate());
                                                  supplyTimeVirtuallMap.put(codes, buildDetailItem.getVirtualFinishDate());
                                              }else if(StringUtils.isNotEmpty(nodeCode) && nodeCode.equals(NodeEnum.JT_LCB_00003.getKey())){
                                                  openSaleTimeMap.put(codes, buildDetailItem.getPlannedFinishDate());
                                                  openSaleTimeActualMap.put(codes, buildDetailItem.getActualFinishDate());
                                              }else if(StringUtils.isNotEmpty(nodeCode) && nodeCode.equals(NodeEnum.JT_YJ_00003.getKey())){
                                                  cappingTimeMap.put(codes, buildDetailItem.getPlannedFinishDate());
                                                  cappingTimeActualMap.put(codes, buildDetailItem.getActualFinishDate());
                                              }else if(StringUtils.isNotEmpty(nodeCode) && nodeCode.equals(NodeEnum.JT_LCB_00004.getKey())){
                                                  completedTimeMap.put(codes, buildDetailItem.getPlannedFinishDate());
                                                  completedTimeActualMap.put(codes, buildDetailItem.getActualFinishDate());
                                              }else if(StringUtils.isNotEmpty(nodeCode) && nodeCode.equals(NodeEnum.JT_LCB_00006.getKey())){
                                                  carryOverTimeMap.put(codes, buildDetailItem.getPlannedFinishDate());
                                                  carryOverTimeActualMap.put(codes, buildDetailItem.getActualFinishDate());
                                              }else if(StringUtils.isNotEmpty(nodeCode) && nodeCode.equals(NodeEnum.JT_LCB_00005.getKey())){
                                                  actualDeliveryTimeMap.put(codes, buildDetailItem.getPlannedFinishDate());
                                                  actualDeliveryTimeActualMap.put(codes, buildDetailItem.getActualFinishDate());
                                              }else if(StringUtils.isNotEmpty(nodeCode) && nodeCode.equals(NodeEnum.NODE_000.getKey())){
                                                  contractDeliveryTimeMap.put(codes, buildDetailItem.getPlannedFinishDate());
                                                  contractDeliveryTimeActualMap.put(codes, buildDetailItem.getActualFinishDate());
                                              }
                                          }
                                      }
                                  }

                                  //分期有可售或阶段性自持产品构成没有批次或供货日期时，不允许新建该分期价格版本
                                  List<PanoplanBuildPlanItem> panoProductItems = panoPlanCastDecisionBO.getBuildItems();
                                  for (int i = 0;i < panoProductItems.size(); i++){
                                      PanoplanBuildPlanItem panoplanBuildPlanItem = panoProductItems.get(i);
                                      String saleNum = panoplanBuildPlanItem.getSaleNum();
                                      if(StringUtils.isNotEmpty(saleNum) && (saleNum.equals(SalyTypeEnum.SALABLE.getKey())
                                              || saleNum.equals(SalyTypeEnum.STAGE_SELF_SUSTAINMENT.getKey()))){
                                          String codes = panoplanBuildPlanItemMap.get(panoplanBuildPlanItem.getId());
                                          LocalDateTime supplyTime = supplyTimeMap.get(codes);
                                          LocalDateTime supplyActualTime = supplyTimeActualMap.get(codes);
                                          LocalDateTime supplyVirtuallTime = supplyTimeVirtuallMap.get(codes);
                                          if(supplyTime == null && supplyActualTime == null && supplyVirtuallTime == null){

                                              transferResult.setStates("2");
                                              transferResult.setRemark("【楼栋计划】分期有可售或阶段性自持产品构成没有批次或供货日期时，不允许新建该分期价格版本");
                                              resultTransferResults.add(transferResult);

                                              continue zwc;
                                          }
                                      }
                                  }

                                  //查询取数系统
                                  LocalDateTime fetchTime = null;
                                  String fetchSysCode = null;
                                  String fetchSysId = null;
                                  SysProjectFetchNumber fetchNumber = sysProjectFetchNumberService.selectByFid(stageId);
                                  if(fetchNumber != null){
                                      if(StringUtils.isNotEmpty(fetchNumber.getCountingSystemId())){
                                          fetchTime = fetchNumber.getEffectiveDate();
                                          fetchSysCode = fetchNumber.getCountingSystemId();
                                          fetchSysId = fetchNumber.getId();
                                      }else{
                                          transferResult.setStates("2");
                                          transferResult.setRemark("【系统配置】取数系统未配置");
                                          resultTransferResults.add(transferResult);
                                          continue zwc;
                                      }
                                  }else{
                                      transferResult.setStates("2");
                                      transferResult.setRemark("【系统配置】取数系统未配置");
                                      resultTransferResults.add(transferResult);

                                      continue zwc;
                                  }

                                  //项目操盘方式
                                  //DULICP=独立操盘-------营销操盘
                                  //LIANHECP=联合操盘-------非营销操盘
                                  //BUCP=不操盘-------非营销操盘
                                  String market = null;
                                  //MdmProjectPatner partner = projectPatnerService.selectByFidAndPartnerNo(mdmStage.getProjectId());
                                  StagePartNers partner = stagePartNersService.getStagePartnersByProjectFid(mdmStage.getProjectFid());
                                  if(partner != null){
                                      String isOpMarketing = partner.getIsOpMarketing();
                                      if(StringUtils.isNotEmpty(isOpMarketing) && (MarketingEunm.LIANHECP.getValue().equals(isOpMarketing) || MarketingEunm.BUCP.getValue().equals(isOpMarketing))){
                                          market = MarketingEunm.IS_NOT_MARKET.getKey();
                                      }else if (StringUtils.isNotEmpty(isOpMarketing) && MarketingEunm.DULICP.getValue().equals(isOpMarketing)){
                                          market = MarketingEunm.IS_MARKET.getKey();
                                      }
                                  }else{
                                      throw new MsgException(CodeEnum.NON_MARKET_ERROR);
                                  }

                                  //归属年月
                                  LocalDate ascriptionDate = LocalDate.now();
                                  Integer isFinalization = null;
                                  if(versionType.equals(VersionTypeEunm.DYNAMIC.getKey())){
                                      isFinalization = IsDeleteEnum.NO.getKey();

                                      int day = now.getDayOfMonth();
                                      int hour = now.getHour();
                                      if (day == Constants.ONE && hour <=  Constants.SIX){//一号
                                          ascriptionDate = ascriptionDate.minusMonths(1);
                                      }
                                  }else{
                                      ascriptionDate = null;
                                  }

                                  //查询上一版本信息，如果是动态版之查当月的
                                  String oldVersionId = "";
                                  String oldVersionNum = "";
                                  PriceVersion priceVersion = null;
                                  if(versionType.equals(VersionTypeEunm.DYNAMIC.getKey())) {
                                      priceVersion = priceVersionService.selectNewestVersionByFidAndDate(stageId, Arrays.asList(versionType), Arrays.asList(VersionStatusEnum.SUCCESSAPPROVED.getKey(), VersionStatusEnum.SYSGENERATION.getKey()), IsDeleteEnum.NO.getKey(), now);
                                  }else{
                                      priceVersion = priceVersionService.selectNewestVersionByFid(stageId, versionType);
                                  }
                                  if(priceVersion != null){
                                      oldVersionId = priceVersion.getId();
                                      oldVersionNum = priceVersion.getVersionNum();
                                  }

                                  //查询全盘最新货值（复制战规后最新货值版本）
                                  String sourceValueId = "";
                                  Map<String, PriceValueOrg> preSaleParkMap = new HashMap<String, PriceValueOrg>();
                                  Map<String, List<PriceValueOrg>> preHouseMap = new HashMap<String, List<PriceValueOrg>>();
                                  Map<String, PriceValueOrg> preNotSaleParkMap = new HashMap<String, PriceValueOrg>();
                                  Map<String, PriceValueOrg> preBuildMap = new HashMap<String, PriceValueOrg>();
                                  PriceVersion newestPriceVersion = priceVersionService.getNewestPriceVersion(mdmStage.getProjectFid());
                                  if(newestPriceVersion != null){
                                      String newestversionType = newestPriceVersion.getVersionType();
                                      if (!newestversionType.equals(VersionTypeEunm.INVESTMENT.getKey())){
                                          sourceValueId = newestPriceVersion.getId();

                                          List<PriceValueCastDecisionAllOrgEXTBO> priceValueCastDecisionAllOrgEXTBOS = priceValueOrgMapper.selectHouseAndPark(newestPriceVersion.getId(), IsDeleteEnum.NO.getKey());
                                          if (CollectionUtils.isNotEmpty(priceValueCastDecisionAllOrgEXTBOS)){
                                              for (PriceValueCastDecisionAllOrgEXTBO preValueOrg : priceValueCastDecisionAllOrgEXTBOS){
                                                  String type = preValueOrg.getType();
                                                  if (type.equals(OrgTypeEunm.ORG_SALING_PARKING.getKey())){
                                                      preSaleParkMap.put(preValueOrg.getCode(), preValueOrg);
                                                  }else if(type.equals(OrgTypeEunm.ORG_HOUSE_PACKAGE.getKey())){
                                                      List<PriceValueOrg> valueOrgs = preHouseMap.get(preValueOrg.getCode());
                                                      if (CollectionUtils.isEmpty(valueOrgs)){
                                                          valueOrgs = new ArrayList<PriceValueOrg>();
                                                      }
                                                      valueOrgs.add(preValueOrg);
                                                      preHouseMap.put(preValueOrg.getCode(), valueOrgs);
                                                  }else if (type.equals(OrgTypeEunm.ORG_NOT_SALING_PARKING.getKey())){
                                                      preNotSaleParkMap.put(preValueOrg.getCode(), preValueOrg);
                                                  }
                                              }
                                          }
                                      }

                                      List<PriceValueOrg> oldBuildList = priceValueOrgService.selectBuildByVersionId(newestPriceVersion.getId(), OrgTypeEunm.ORG_BUILD.getKey(), null, IsDeleteEnum.NO.getKey());
                                      if (CollectionUtils.isNotEmpty(oldBuildList)){
                                          for (PriceValueOrg oldBuild : oldBuildList){
                                              preBuildMap.put(oldBuild.getOrgId(), oldBuild);
                                          }
                                      }
                                  }

                                  //创建新版本
                                  String newVersionId = UUIDUtils.create();//新版本ID
                                  PriceVersion newVersion = new PriceVersion();
                                  String newVersionNum = VersionUtils.bigVersionInc(oldVersionNum);
                                  newVersion.setPreVersionId(oldVersionId);
                                  newVersion.setPreVersionNum(oldVersionNum);
                                  newVersion.setId(newVersionId);
                                  newVersion.setProjectId(mdmStage.getProjectId());
                                  newVersion.setProjectfId(stageId);
                                  newVersion.setStageName(mdmStage.getStageName());
                                  newVersion.setStageCode(mdmStage.getStageCode());

                                  //版本名称规则：
                                  //1、月度动态版：
                                  //规则： {归属月份}月动态版{版本号}-{最后更新时间}
                                  //示例：201910月动态版V1-20191201；
                                  //2、投资版、战规版、大定价版：
                                  //规则：{版本类型}{版本号}-{最后更新时间}
                                  //示例：投资版V1-20191102，战规版V3-20191102，大定价版V2-20191102
                                  if (versionType.equals(VersionTypeEunm.DYNAMIC.getKey())){
                                      DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMM");
                                      newVersion.setVersionName(df.format(ascriptionDate)+"月动态版"+newVersionNum);
                                  }else{
                                      newVersion.setVersionName(VersionTypeEunm.getValueByKey(versionType)+newVersionNum);
                                  }

                                  newVersion.setVersionNum(newVersionNum);
                                  newVersion.setVersionType(versionType);
                                  newVersion.setVersionCreateState(VersionCreateEnum.MANUALCREATE.getKey());
                                  newVersion.setCreateBy(Constants.SYS);
                                  newVersion.setVersionState(VersionStatusEnum.NOTPREPARED.getKey());
                                  newVersion.setRemake("");

                                  newVersion.setSourceAreaVersionStage(mdmProductWarPlanBO.getSourceAreaStage());
                                  newVersion.setSourceAreaVersionNum(mdmProductWarPlanBO.getSourceAreaNum());
                                  newVersion.setSourceAreaCreatedTime(mdmProductWarPlanBO.getSourceAreaCreatedTime());
                                  newVersion.setSourceAreaSysCreatedTime(mdmProductWarPlanBO.getSourceAreaSysCreatedTime());

                                  newVersion.setSourceGroupVersionStage(mdmProductWarPlanBO.getSourceGroupStage());
                                  newVersion.setSourceGroupVersionCode(mdmProductWarPlanBO.getSourceGroupNum());
                                  newVersion.setSourceGroupApprovalTime(mdmProductWarPlanBO.getSourceGroupApproval());
                                  newVersion.setSourceGroupCreateTime(mdmProductWarPlanBO.getSourceGroupSysCreatedTime());

                                  String sourceAreaDetail = "["+mdmProductWarPlanBO.getSourceAreaStage()+"&"+mdmProductWarPlanBO.getSourceAreaNum()+"&"+mdmProductWarPlanBO.getSourceAreaCreatedTime()+"]";
                                  String sourceOrgDetail = "["+mdmProductWarPlanBO.getSourceGroupStage()+"&"+mdmProductWarPlanBO.getSourceGroupNum()+"&"+mdmProductWarPlanBO.getSourceGroupApproval()+"]";
                                  newVersion.setSourceArea(sourceAreaDetail+"-"+sourceOrgDetail);
                                  newVersion.setSourceAreaDetail("["+ DateUtils.dateToString(LocalDateTimeUtils.convertLDTToDate(mdmProductWarPlanBO.getProjectFItems().getCreatDate()),
                                          DateUtils.DATE_KEY_SMALL_STR) +"]-["+mdmProductWarPlanBO.getProjectFItems().getVersionStage()+"]-["+mdmProductWarPlanBO.getProjectFItems().getVersionNum()+"]");

                                  newVersion.setSourcePlan(panoPlanCastDecisionBO.getPanoplanBuildPlan().getId());
                                  newVersion.setSourcePlanDetail("["+DateUtils.dateToString(LocalDateTimeUtils.convertLDTToDate(panoPlanCastDecisionBO.getPanoplanBuildPlan().getCreatedDate()) ,
                                          DateUtils.DATE_KEY_SMALL_STR) + "]-[" +  panoPlanCastDecisionBO.getPanoplanBuildPlan().getVersionType() +"]-[" +
                                          panoPlanCastDecisionBO.getPanoplanBuildPlan().getPlanVersion()+"]");
                                  newVersion.setSourcePanoVersionType(panoPlanCastDecisionBO.getPanoplanBuildPlan().getVersionType());
                                  newVersion.setSourcePanoPlanVersion(panoPlanCastDecisionBO.getPanoplanBuildPlan().getPlanVersion());
                                  newVersion.setSourcePanoCreatedDate(panoPlanCastDecisionBO.getPanoplanBuildPlan().getCreatedDate());

                                  newVersion.setIsFinalization(isFinalization);
                                  newVersion.setAscriptionDate(ascriptionDate);
                                  newVersion.setSourceValue(sourceValueId);
                                  newVersion.setCountingTime(fetchTime);
                                  newVersion.setCountingSystemCode(fetchSysCode);
                                  newVersion.setCountingSystemId(fetchSysId);
                                  newVersion.setIsMarket(market);
                                  newVersion.setIsDelete(IsDeleteEnum.NO.getKey());
                                  newVersion.setCreateTime(now);
                                  newVersion.setUpdateTime(now);

                                  //创建分期
                                  PriceValueOrg projectOrg = new PriceValueOrg();
                                  String projectOrgId = UUIDUtils.create();
                                  projectOrg.setId(projectOrgId);
                                  projectOrg.setVersionId(newVersionId);
                                  projectOrg.setPid("0");
                                  projectOrg.setType(OrgTypeEunm.ORG_STAGE.getKey());
                                  projectOrg.setOrgId(mdmStage.getProjectFid());
                                  projectOrg.setOrgCode(mdmStage.getStageCode());
                                  projectOrg.setOrgName(mdmStage.getStageName());
                                  projectOrg.setIsDelete(IsDeleteEnum.NO.getKey());
                                  projectOrg.setCreateTime(now);
                                  projectOrg.setUpdateTime(now);

                                  //创建组团
                                  Map<String, PriceValueOrg> groupMap = new HashMap<String, PriceValueOrg>();
                                  Map<String, PriceValueOrg> groupInsertMap = new HashMap<String, PriceValueOrg>();
                                  List<MdmBuildAreaDetailBO> groupItems = mdmProductWarPlanBO.getGroupItems();
                                  for(MdmBuildAreaDetailBO groupItem : groupItems){
                                      PriceValueOrg priceValueOrg = new PriceValueOrg();
                                      String groupId = UUIDUtils.create();
                                      priceValueOrg.setId(groupId);
                                      priceValueOrg.setVersionId(newVersionId);
                                      priceValueOrg.setPid(projectOrgId);
                                      priceValueOrg.setType(OrgTypeEunm.ORG_GROUP.getKey());
                                      priceValueOrg.setOrgId(groupItem.getGroupId());
                                      priceValueOrg.setOrgCode(groupItem.getGroupCode());
                                      priceValueOrg.setOrgName(groupItem.getGroupName());
                                      priceValueOrg.setIsParkingGroup(groupItem.getIsParkingGroup());
                                      priceValueOrg.setIsDelete(IsDeleteEnum.NO.getKey());
                                      priceValueOrg.setCreateTime(now);
                                      priceValueOrg.setUpdateTime(now);

                                      groupMap.put(groupItem.getGroupId(), priceValueOrg);
                                      groupInsertMap.put(groupId, priceValueOrg);
                                  }

                                  //创建产品构成
                                  Map<String, PriceValueOrg> productMap = new HashMap<String, PriceValueOrg>();
                                  Map<String, PriceValueOrg> productInsertMap = new HashMap<String, PriceValueOrg>();
                                  List<MdmBuildAreaDetailBO> productItems = mdmProductWarPlanBO.getProductItems();

                                  //寻找上级分类
                                  Map<String, DictionaryCodeBO> dicMap = new HashMap();
                                  List<String> productCodes = Optional.of(productItems).orElseGet(ArrayList::new)
                                          .stream().map(MdmBuildAreaDetailBO::getProductCode)
                                          .collect(Collectors.toList());
                                  if(CollectionUtils.isNotEmpty(productCodes)){
                                      List<DictionaryCodeBO> dicType = sysDictionaryService.selectByCodes(productCodes);
                                      if(CollectionUtils.isNotEmpty(dicType)){
                                          for(DictionaryCodeBO bo : dicType){
                                              dicMap.put(bo.getChildCode(),bo);
                                          }
                                      }
                                  }

                                  for(MdmBuildAreaDetailBO productItem : productItems){
                                      PriceValueOrg priceValueOrg = new PriceValueOrg();
                                      String productId = UUIDUtils.create();
                                      priceValueOrg.setId(productId);
                                      priceValueOrg.setVersionId(newVersionId);
                                      priceValueOrg.setPid(groupMap.get(productItem.getGroupId()).getId());
                                      priceValueOrg.setType(OrgTypeEunm.ORG_PRODUCT.getKey());
                                      priceValueOrg.setOrgId("");
                                      priceValueOrg.setOrgCode(productItem.getProductCode());
                                      priceValueOrg.setOrgName(productItem.getProductType());
                                      priceValueOrg.setProductCode(productItem.getProductCode());
                                      priceValueOrg.setProductType(productItem.getProductType());
                                      DictionaryCodeBO dictionaryCodeBO = dicMap.get(productItem.getProductCode());
                                      if(dictionaryCodeBO != null){
                                          priceValueOrg.setProProductCode(dictionaryCodeBO.getParentCode());
                                          priceValueOrg.setProProductType(dictionaryCodeBO.getParentName());
                                      }
                                      if(isParkList.contains(productItem.getProductCode())){
                                          priceValueOrg.setIsParking(IsDeleteEnum.YES.getKey());
                                      }else{
                                          priceValueOrg.setIsParking(IsDeleteEnum.NO.getKey());
                                      }
                                      priceValueOrg.setIsDelete(IsDeleteEnum.NO.getKey());
                                      priceValueOrg.setCreateTime(now);
                                      priceValueOrg.setUpdateTime(now);

                                      productMap.put(productItem.getGroupId() + "&" + productItem.getProductCode(), priceValueOrg);
                                      productInsertMap.put(productId, priceValueOrg);
                                  }

                                  //创建楼栋
                                  List<String> parkingBuildList = new ArrayList<>();
                                  List<PriceValueOrg> buildList = new ArrayList<PriceValueOrg>();
                                  for(MdmBuildAreaDetailBO buildItem : mdmProductWarPlanBO.getBuildItems()){
                                      PriceValueOrg priceValueOrg = new PriceValueOrg();
                                      String buildId = UUIDUtils.create();
                                      priceValueOrg.setId(buildId);
                                      priceValueOrg.setVersionId(newVersionId);
                                      PriceValueOrg productOrg = productMap.get(buildItem.getGroupId() + "&" + buildItem.getProductCode());
                                      priceValueOrg.setPid(productOrg.getId());
                                      priceValueOrg.setType(OrgTypeEunm.ORG_BUILD.getKey());
                                      priceValueOrg.setOrgId(buildItem.getBldPrdId());
                                      priceValueOrg.setOrgCode(buildItem.getDesignBuildId());
                                      priceValueOrg.setOrgName(buildItem.getFormatBuildingName());

                                      if(StringUtils.isNotEmpty(buildItem.getBusinessType()) && StringUtils.isNotEmpty(buildItem.getBusinessTypeName())){
                                          priceValueOrg.setBusinessType(buildItem.getBusinessType());
                                          priceValueOrg.setBusinessTypeName(buildItem.getBusinessTypeName());
                                      }else{

                                          transferResult.setStates("2");
                                          transferResult.setRemark("【主数据】分期中经营方式为空");
                                          resultTransferResults.add(transferResult);

                                          continue;
                                      }
                                      if(StringUtils.isNotEmpty(buildItem.getFreeType()) && StringUtils.isNotEmpty(buildItem.getFreeTypeName())){
                                          priceValueOrg.setFreeType(buildItem.getFreeType());
                                          priceValueOrg.setFreeTypeName(buildItem.getFreeTypeName());
                                      }else{

                                          transferResult.setStates("2");
                                          transferResult.setRemark("【主数据】分期中装修类型为空");
                                          resultTransferResults.add(transferResult);

                                          continue;
                                      }
                                      if(StringUtils.isNotEmpty(buildItem.getProType()) && StringUtils.isNotEmpty(buildItem.getProTypeName())){
                                          priceValueOrg.setProType(buildItem.getProType());
                                          priceValueOrg.setProTypeName(buildItem.getProTypeName());
                                      }else{

                                          transferResult.setStates("2");
                                          transferResult.setRemark("【主数据】分期中属性标签为空");
                                          resultTransferResults.add(transferResult);

                                          continue;
                                      }

                                      priceValueOrg.setSaleArea(buildItem.getTotalSaleArea()==null?new BigDecimal(0):buildItem.getTotalSaleArea());
                                      priceValueOrg.setNum(buildItem.getRoomNum()==null?0:buildItem.getRoomNum());
                                      priceValueOrg.setAreaTotalSaleArea(buildItem.getTotalSaleArea());
                                      priceValueOrg.setAreaRoomNum(new BigDecimal(buildItem.getRoomNum()==null?0:buildItem.getRoomNum()));

                                      //匹配时间节点
                                      String strCode = buildItem.getGroupId() + "&" + buildItem.getProductCode() + "&" + buildItem.getBldPrdId();
                                      priceValueOrg.setOpenTime(openTimeMap.get(strCode));
                                      priceValueOrg.setOpenTimeActual(openTimeActualMap.get(strCode));
                                      priceValueOrg.setSupplyTime(supplyTimeMap.get(strCode));
                                      priceValueOrg.setSupplyTimeActual(supplyTimeActualMap.get(strCode));
                                      priceValueOrg.setSupplyTimeVirtual(supplyTimeVirtuallMap.get(strCode));
                                      priceValueOrg.setAdvanceSaleTime(advanceSaleTimeMap.get(strCode));
                                      priceValueOrg.setAdvanceSaleTimeActual(advanceSaleTimeActualMap.get(strCode));
                                      priceValueOrg.setOpenSaleTime(openSaleTimeMap.get(strCode));
                                      priceValueOrg.setOpenSaleTimeActual(openSaleTimeActualMap.get(strCode));
                                      priceValueOrg.setCappingTime(cappingTimeMap.get(strCode));
                                      priceValueOrg.setCappingTimeActual(cappingTimeActualMap.get(strCode));
                                      priceValueOrg.setCompletedTime(completedTimeMap.get(strCode));
                                      priceValueOrg.setCompletedTimeActual(completedTimeActualMap.get(strCode));
                                      priceValueOrg.setCarryOverTime(carryOverTimeMap.get(strCode));
                                      priceValueOrg.setCarryOverTimeActual(carryOverTimeActualMap.get(strCode));
                                      priceValueOrg.setActualDeliveryTime(actualDeliveryTimeMap.get(strCode));
                                      priceValueOrg.setActualDeliveryTimeActual(actualDeliveryTimeActualMap.get(strCode));
                                      priceValueOrg.setContractDeliveryTime(contractDeliveryTimeMap.get(strCode));
                                      priceValueOrg.setContractDeliveryTimeActual(contractDeliveryTimeActualMap.get(strCode));

                                      PanoplanBuildPlanItem panoplanItem = panoplanItemMap.get(buildItem.getGroupId() + "&" + buildItem.getProductCode() + "&" + buildItem.getBldPrdId());
                                      if(panoplanItem != null){
                                          priceValueOrg.setPanoTotalRoomNum(panoplanItem.getTotalRoomNum());
                                          priceValueOrg.setPanoTotalSaleArea(panoplanItem.getTotalSaleArea());
                                          priceValueOrg.setPanoBatchRoomNum(panoplanItem.getBatchRoomNum());
                                          priceValueOrg.setPanoBatchSaleArea(panoplanItem.getBatchSaleArea());
                                          priceValueOrg.setPanoSalePark(panoplanItem.getSalePark());
                                          priceValueOrg.setPanoTotalBuildArea(panoplanItem.getTotalBuildArea());
                                          priceValueOrg.setPanoBatchBuildArea(panoplanItem.getBatchBuildArea());
                                      }

                                      priceValueOrg.setIsDelete(IsDeleteEnum.NO.getKey());
                                      priceValueOrg.setCreateTime(now);

                                      LocalDateTime ghshTime = supplyTimeVirtuallMap.get(strCode);
                                      if(ghshTime != null){
                                          if(now.isBefore(ghshTime)){
                                              priceValueOrg.setIsSupply(SupplyEnum.NO.getKey());
                                          }else{
                                              priceValueOrg.setIsSupply(SupplyEnum.YES.getKey());
                                          }
                                      }else{
                                          ghshTime = supplyTimeActualMap.get(strCode);
                                          if(ghshTime != null){
                                              if(now.isBefore(ghshTime)){
                                                  priceValueOrg.setIsSupply(SupplyEnum.NO.getKey());
                                              }else{
                                                  priceValueOrg.setIsSupply(SupplyEnum.YES.getKey());
                                              }
                                          }else{
                                              priceValueOrg.setIsSupply(SupplyEnum.NO.getKey());
                                          }
                                      }

                                      String productCode = productOrg.getProductCode();
                                      if(isParkList.contains(productCode)){
                                          priceValueOrg.setIsParking(IsDeleteEnum.YES.getKey());
                                          priceValueOrg.setParkingNum(buildItem.getRoomNum()==null?0:buildItem.getRoomNum());
                                          parkingBuildList.add(buildItem.getBldPrdId());
                                      }else{
                                          priceValueOrg.setRoomNum(buildItem.getRoomNum()==null?0:buildItem.getRoomNum());
                                          priceValueOrg.setIsParking(IsDeleteEnum.NO.getKey());
                                      }

                                      buildList.add(priceValueOrg);
                                  }

                                  //查看取数系统
                                  Map<String, PriceValueOrg> soldParkingMap = new HashMap<String, PriceValueOrg>();
                                  Map<String, PriceValueOrg> notSoldParkingOrgMap = new HashMap<String, PriceValueOrg>();
                                  Map<String, Map<String ,PriceValueOrg>> houseOrgMap = new HashMap<String, Map<String ,PriceValueOrg>>();

                                  if (fetchSysCode.equals(FetchNnumberEnum.MY.getKey())){

                                      //收集面积段
//                                      Map<String, Map> houseParkMap = priceVersionValueOrgServiceImpl.myCollectHouse(stageId, mdmStage.getKingdeeProjectfId(), parkingBuildList, market, now,null);
//                                      soldParkingMap = houseParkMap.get("soldParkingMap");
//                                      notSoldParkingOrgMap = houseParkMap.get("notSoldParkingOrgMap");
//                                      houseOrgMap = houseParkMap.get("houseOrgMap");

                                  }else if(fetchSysCode.equals(FetchNnumberEnum.NOS.getKey())){
                                      Map<String, Map> houseParkMap = priceVersionValueOrgServiceImpl.nosCollectHouse(mdmStage.getKingdeeProjectfId(), now,null);
                                      soldParkingMap = houseParkMap.get("soldParkingMap");
                                      houseOrgMap = houseParkMap.get("houseOrgMap");
                                  }

                                  //是否有销售货值
                                  if (CollectionUtils.isNotEmpty(houseOrgMap)){
                                      newVersion.setIsContainSale(IsDeleteEnum.YES.getKey());
                                  }else{
                                      newVersion.setIsContainSale(IsDeleteEnum.NO.getKey());
                                  }

                                  List<PriceValueOrg> buildInsertList = new ArrayList<PriceValueOrg>();
                                  List<PriceValueOrg> hoseSoleList = new ArrayList<PriceValueOrg>();
                                  List<PriceValueOrg> sortParkList = new ArrayList<PriceValueOrg>();

                                  //归集金蝶面积段
                                  //收集面积段
                                  Map<String, Map<String, TransferKingdeeValue>> jdMap = jdCollectHouse(mdmStage.getStageCode(), parkingBuildList);
                                  Map<String, TransferKingdeeValue> jdSoldHouse = jdMap.get("jdSoldHouse");
                                  Map<String, TransferKingdeeValue> jdSoldParking = jdMap.get("jdSoldParking");

                                  //赋值楼栋货值
                                  wc: for (PriceValueOrg buildOrg : buildList){
                                      String buildId = buildOrg.getId();
                                      String buildOrgId = buildOrg.getOrgId();
                                      Integer isParking = buildOrg.getIsParking();
                                      Integer buildNum = buildOrg.getNum()==null?0:buildOrg.getNum();
                                      String businessType = buildOrg.getBusinessType();
                                      BigDecimal buildAveragePriceParking = buildOrg.getAveragePriceParking();

                                      //如果楼栋是 不可租售 或 可租，则不复制信息
                                      if (businessType.equals(SalyTypeEnum.NOT_RENTABLE.getKey()) || businessType.equals(SalyTypeEnum.RENTED.getKey())){
                                          buildInsertList.add(buildOrg);
                                          continue wc;
                                      }

                                      //车位信息
                                      if(isParking == IsDeleteEnum.YES.getKey()){
                                          buildOrg.setParkingNum(buildNum);

                                          //赋值金蝶车位楼栋信息
                                          TransferKingdeeValue transferParking = jdSoldParking.get(buildOrg.getOrgId());
                                          BigDecimal transferAvgPrice = null;
                                          if (transferParking != null){
                                              transferAvgPrice = transferParking.getAvgPrice();

                                              buildOrg.setAveragePriceParking(transferAvgPrice);
                                              buildOrg.setNotCommodityValue(priceVersionValueOrgServiceImpl.BigDecimalMUL(transferAvgPrice, new BigDecimal(buildOrg.getNum())) );
                                          }

                                          int collectNum = 0; //归集总已售车位数量
                                          BigDecimal collectPirce = new BigDecimal(0); //归集总已售车位货值

                                          int saleSumNum = 0; //已售车位数量
                                          BigDecimal saleSumPrice = new BigDecimal(0); //已售车位金额

                                          int notSaleSumNum = 0; //已定价未售车位数量
                                          BigDecimal notSaleSumPrice = new BigDecimal(0); //已定价未售车位金额

                                          //归集已售车位
                                          if(CollectionUtils.isNotEmpty(soldParkingMap)){
                                              PriceValueOrg solePark = soldParkingMap.get(buildOrg.getOrgId());
                                              if (solePark != null) {
                                                  solePark.setPid(buildOrg.getId());
                                                  solePark.setVersionId(buildOrg.getVersionId());
                                                  solePark.setUpdateTime(now);
                                                  sortParkList.add(solePark);

                                                  collectNum = collectNum + solePark.getNum();
                                                  collectPirce = priceVersionValueOrgServiceImpl.BigDecimalADD(collectPirce, solePark.getNotCommodityValue());

                                                  saleSumNum = saleSumNum + solePark.getNum();
                                                  saleSumPrice = priceVersionValueOrgServiceImpl.BigDecimalADD(saleSumPrice, solePark.getNotCommodityValue());
                                              }
                                          }

                                          //归集已定价未售车位
                                          if(CollectionUtils.isNotEmpty(notSoldParkingOrgMap)){
                                              PriceValueOrg notSolePark = notSoldParkingOrgMap.get(buildOrg.getOrgId());
                                              if (notSolePark != null) {
                                                  notSolePark.setPid(buildOrg.getId());
                                                  notSolePark.setVersionId(buildOrg.getVersionId());
                                                  notSolePark.setUpdateTime(now);
                                                  sortParkList.add(notSolePark);

                                                  collectNum = collectNum + notSolePark.getNum();
                                                  collectPirce = priceVersionValueOrgServiceImpl.BigDecimalADD(collectPirce, notSolePark.getNotCommodityValue());

                                                  notSaleSumNum = notSaleSumNum + notSolePark.getNum();
                                                  notSaleSumPrice = priceVersionValueOrgServiceImpl.BigDecimalADD(notSaleSumPrice, notSolePark.getNotCommodityValue());
                                              }
                                          }

                                          int subNum = buildNum - collectNum;
                                          if (subNum > 0) {

                                              //归集金蝶可售车位
                                              if (transferParking != null){
                                                  //有货值的作为可售车位处理
                                                  if (transferAvgPrice != null && transferAvgPrice.compareTo(new BigDecimal(0)) > 0){
                                                      PriceValueOrg newPriceValueOrg = new PriceValueOrg();

                                                      newPriceValueOrg.setId(UUIDUtils.create());
                                                      newPriceValueOrg.setVersionId(newVersionId);
                                                      newPriceValueOrg.setPid(buildId);
                                                      newPriceValueOrg.setType(OrgTypeEunm.ORG_SALING_PARKING.getKey());
                                                      newPriceValueOrg.setOrgName(OrgTypeEunm.ORG_SALING_PARKING.getValue());
                                                      newPriceValueOrg.setNum(subNum);
                                                      newPriceValueOrg.setParkingNum(subNum);
                                                      newPriceValueOrg.setAveragePriceParking(transferAvgPrice);
                                                      newPriceValueOrg.setNotCommodityValue(priceVersionValueOrgServiceImpl.BigDecimalMUL(new BigDecimal(subNum), transferAvgPrice));
                                                      newPriceValueOrg.setIsDelete(IsDeleteEnum.NO.getKey());
                                                      newPriceValueOrg.setCreateTime(now);
                                                      newPriceValueOrg.setUpdateTime(now);

                                                      sortParkList.add(newPriceValueOrg);

                                                      collectNum = collectNum + subNum;
                                                      collectPirce = priceVersionValueOrgServiceImpl.BigDecimalADD(collectPirce, newPriceValueOrg.getNotCommodityValue());

                                                      subNum = 0;
                                                  }else{
                                                      //无货值的作为未推售车位处理
                                                      PriceValueOrg newSalePriceValueOrg = new PriceValueOrg();

                                                      newSalePriceValueOrg.setId(UUIDUtils.create());
                                                      newSalePriceValueOrg.setVersionId(newVersionId);
                                                      newSalePriceValueOrg.setPid(buildId);
                                                      newSalePriceValueOrg.setType(OrgTypeEunm.ORG_SALING_PARKING.getKey());
                                                      newSalePriceValueOrg.setOrgName(OrgTypeEunm.ORG_SALING_PARKING.getValue());
                                                      newSalePriceValueOrg.setNum(0);
                                                      newSalePriceValueOrg.setParkingNum(0);
                                                      newSalePriceValueOrg.setAveragePriceParking(new BigDecimal(0));
                                                      newSalePriceValueOrg.setNotCommodityValue(new BigDecimal(0));
                                                      newSalePriceValueOrg.setIsDelete(IsDeleteEnum.NO.getKey());
                                                      newSalePriceValueOrg.setCreateTime(now);
                                                      newSalePriceValueOrg.setUpdateTime(now);

                                                      sortParkList.add(newSalePriceValueOrg);

                                                      //待售车位信息
                                                      PriceValueOrg newNotSalePriceValueOrg = new PriceValueOrg();

                                                      newNotSalePriceValueOrg.setId(UUIDUtils.create());
                                                      newNotSalePriceValueOrg.setVersionId(newVersionId);
                                                      newNotSalePriceValueOrg.setPid(buildId);
                                                      newNotSalePriceValueOrg.setType(OrgTypeEunm.ORG_NOT_SALING_PARKING.getKey());
                                                      newNotSalePriceValueOrg.setOrgName(OrgTypeEunm.ORG_NOT_SALING_PARKING.getValue());
                                                      newNotSalePriceValueOrg.setNum(subNum);
                                                      newNotSalePriceValueOrg.setParkingNum(subNum);
                                                      newNotSalePriceValueOrg.setCommodityValue(priceVersionValueOrgServiceImpl.BigDecimalMUL(new BigDecimal(subNum), buildOrg.getAveragePriceParking()));
                                                      newNotSalePriceValueOrg.setIsDelete(IsDeleteEnum.NO.getKey());
                                                      newNotSalePriceValueOrg.setCreateTime(now);
                                                      newNotSalePriceValueOrg.setUpdateTime(now);

                                                      sortParkList.add(newNotSalePriceValueOrg);

                                                      buildOrg.setCommodityValue(newNotSalePriceValueOrg.getCommodityValue());
                                                      buildOrg.setForsaleParkingNum(subNum);

                                                      subNum = 0;
                                                  }

                                                  buildOrg.setAveragePriceParking(priceVersionValueOrgServiceImpl.bigDecimalDIV(collectPirce, new BigDecimal(collectNum)));
                                                  buildOrg.setNotCommodityValue(collectPirce);
                                                  buildOrg.setParkingNum(collectNum);
                                              }

                                              if(subNum == buildNum){
                                                  //楼栋下没有车位则生成一个
                                                  PriceValueOrg newPriceValueOrg = new PriceValueOrg();

                                                  newPriceValueOrg.setId(UUIDUtils.create());
                                                  newPriceValueOrg.setVersionId(newVersionId);
                                                  newPriceValueOrg.setPid(buildId);
                                                  newPriceValueOrg.setType(OrgTypeEunm.ORG_SALING_PARKING.getKey());
                                                  newPriceValueOrg.setOrgName(OrgTypeEunm.ORG_SALING_PARKING.getValue());
                                                  newPriceValueOrg.setNum(subNum);
                                                  newPriceValueOrg.setParkingNum(subNum);
                                                  newPriceValueOrg.setNotCommodityValue(priceVersionValueOrgServiceImpl.BigDecimalMUL(new BigDecimal(subNum), newPriceValueOrg.getAveragePriceParking()));
                                                  newPriceValueOrg.setIsDelete(IsDeleteEnum.NO.getKey());
                                                  newPriceValueOrg.setCreateTime(now);
                                                  newPriceValueOrg.setUpdateTime(now);

                                                  sortParkList.add(newPriceValueOrg);

                                                  buildOrg.setAveragePriceParking(priceVersionValueOrgServiceImpl.bigDecimalDIV(collectPirce, new BigDecimal(collectNum)));
                                                  buildOrg.setNotCommodityValue(collectPirce);
                                                  buildOrg.setParkingNum(subNum);

                                                  buildOrg.setForsaleParkingNum(0);
                                                  buildOrg.setCommodityValue(new BigDecimal(0));
                                              }

                                              buildOrg.setParkingSold(saleSumNum);
                                              buildOrg.setPriceSold(saleSumPrice);
                                              buildOrg.setParkingPriced(notSaleSumNum);
                                              buildOrg.setPricePriced(notSaleSumPrice);
                                              buildInsertList.add(buildOrg);
                                          }else{
                                              //从新计算楼栋的面积、套数、均价、金额(货值)
                                              buildOrg.setNum(collectNum);
                                              buildOrg.setParkingNum(collectNum);
                                              buildOrg.setForsaleParkingNum(0);
                                              buildOrg.setNotCommodityValue(collectPirce);
                                              buildOrg.setCommodityValue(new BigDecimal(0));
                                              buildOrg.setAveragePriceParking(priceVersionValueOrgServiceImpl.bigDecimalDIV(collectPirce, new BigDecimal(subNum)));
                                              buildOrg.setParkingSold(saleSumNum);
                                              buildOrg.setPriceSold(saleSumPrice);
                                              buildOrg.setParkingPriced(notSaleSumNum);
                                              buildOrg.setPricePriced(notSaleSumPrice);
                                              buildInsertList.add(buildOrg);
                                          }
                                      }else{
                                          buildOrg.setRoomNum(buildNum);

                                          //面积段汇总货值
                                          BigDecimal sumPrice = new BigDecimal(0);
                                          BigDecimal sumArea = new BigDecimal(0);

                                          //可售面积段
                                          Integer sumSoleNum = 0;
                                          BigDecimal sumSolePrice = new BigDecimal(0);
                                          BigDecimal sumSoleArea = new BigDecimal(0);

                                          //已定价未售
                                          Integer sumNotSaleNum = 0;
                                          BigDecimal sumNotSaleArea = new BigDecimal(0);
                                          BigDecimal sumNotSalePrice = new BigDecimal(0);

                                          //归集楼栋信息
                                          BigDecimal roomArea = new BigDecimal(0);
                                          Integer roomNum = 0;

                                          //查看金蝶房源楼栋数据信息
                                          TransferKingdeeValue transferHouse = jdSoldHouse.get(buildOrg.getOrgId());
                                          BigDecimal transferAvgPrice = null;
                                          if (transferHouse != null){
                                              transferAvgPrice = transferHouse.getAvgPrice();

                                              buildOrg.setAveragePriceHouse(transferAvgPrice);
                                              buildOrg.setHouseCommodityValue(priceVersionValueOrgServiceImpl.BigDecimalMUL(transferAvgPrice, buildOrg.getSaleArea()));
                                          }

                                          //归集收集房源包
                                          List<String> roomAreaPartList = new ArrayList<String>();
                                          Map<String, PriceValueOrg> roomPriceValueOrgMap = houseOrgMap.get(buildOrgId);
                                          if(CollectionUtils.isNotEmpty(roomPriceValueOrgMap)){
                                              for (Map.Entry<String, PriceValueOrg> roomHouses : roomPriceValueOrgMap.entrySet()){
                                                  PriceValueOrg roomValueOrg = roomHouses.getValue();
                                                  roomValueOrg.setPid(buildId);
                                                  roomValueOrg.setVersionId(buildOrg.getVersionId());
                                                  roomValueOrg.setUpdateTime(now);

                                                  hoseSoleList.add(roomValueOrg);
                                                  roomAreaPartList.add(roomValueOrg.getAreaPart());

                                                  roomArea = priceVersionValueOrgServiceImpl.BigDecimalADD(roomArea, roomValueOrg.getSaleArea());
                                                  roomNum = roomNum + (roomValueOrg.getRoomNum()==null?0:roomValueOrg.getRoomNum());

                                                  sumPrice = priceVersionValueOrgServiceImpl.BigDecimalADD(sumPrice, roomValueOrg.getHouseCommodityValue());
                                                  sumArea = priceVersionValueOrgServiceImpl.BigDecimalADD(sumArea, roomValueOrg.getSaleArea());

                                                  sumSolePrice = priceVersionValueOrgServiceImpl.BigDecimalADD(sumSolePrice, roomValueOrg.getPriceSold());
                                                  sumSoleArea = priceVersionValueOrgServiceImpl.BigDecimalADD(sumSoleArea, roomValueOrg.getAreaSold());
                                                  sumSoleNum = sumSoleNum + (roomValueOrg.getRoomSold()==null?0:roomValueOrg.getRoomSold());

                                                  sumNotSaleNum = sumNotSaleNum + (roomValueOrg.getRoomPriced()==null?0:roomValueOrg.getRoomPriced());
                                                  sumNotSaleArea = priceVersionValueOrgServiceImpl.BigDecimalADD(sumNotSaleArea, roomValueOrg.getAreaPriced());
                                                  sumNotSalePrice = priceVersionValueOrgServiceImpl.BigDecimalADD(sumNotSalePrice, roomValueOrg.getPricePriced());
                                              }
                                          }

                                          //排序
                                          if (CollectionUtils.isNotEmpty(hoseSoleList)){
                                              hoseSoleList.sort((a,b)->{
                                                  String aAreaPart = a.getAreaPart();
                                                  String bAreaPart = b.getAreaPart();

                                                  Integer aComStr = 0;
                                                  Integer bComStr = 0;

                                                  if(aAreaPart.equals(Constants.ORTHER_HOUSE_NAME)){
                                                      aComStr = -1;
                                                  }else{
                                                      String[] aAplitStr = aAreaPart.split("-");
                                                      aComStr = Integer.valueOf(aAplitStr[0]);
                                                  }

                                                  if (bAreaPart.equals(Constants.ORTHER_HOUSE_NAME)){
                                                      bComStr = -1;
                                                  }else{
                                                      String[] bSplitStr = bAreaPart.split("-");
                                                      bComStr = Integer.valueOf(bSplitStr[0]);
                                                  }

                                                  return aComStr.compareTo(bComStr);
                                              });
                                          }

                                          //判断面积
                                          BigDecimal buildArea = buildOrg.getSaleArea()==null?new BigDecimal(0):buildOrg.getSaleArea();
                                          BigDecimal surplusArea = StrUtils.BigDecimalSub(roomArea, buildArea);

                                          //判断套数
                                          int surplusNum = roomNum - buildNum;

                                          if(surplusArea.compareTo(new BigDecimal(0)) < 0 && surplusNum < 0){

                                              BigDecimal subArea = (buildArea).subtract(roomArea);
                                              int subNum = buildNum - roomNum;

                                              //归集其它面积段
                                              PriceValueOrg otherOrg = new PriceValueOrg();
                                              otherOrg.setId(UUIDUtils.create());
                                              otherOrg.setPid(buildOrg.getId());
                                              otherOrg.setVersionId(newVersionId);
                                              otherOrg.setAreaPart(Constants.ORTHER_HOUSE_NAME);
                                              otherOrg.setType(OrgTypeEunm.ORG_HOUSE_PACKAGE.getKey());
                                              otherOrg.setOrgCode(OrgTypeEunm.ORG_HOUSE_PACKAGE.getKey());
                                              otherOrg.setOrgName(Constants.ORTHER_HOUSE_NAME);
                                              otherOrg.setSaleArea(subArea);
                                              otherOrg.setNum(subNum);
                                              otherOrg.setRoomNum(subNum);
                                              otherOrg.setIsDelete(IsDeleteEnum.NO.getKey());
                                              otherOrg.setAveragePriceHouse(transferAvgPrice);
                                              otherOrg.setHouseCommodityValue(priceVersionValueOrgServiceImpl.BigDecimalMUL(subArea, transferAvgPrice));

                                              hoseSoleList.add(otherOrg);
                                              sumPrice = priceVersionValueOrgServiceImpl.BigDecimalADD(sumPrice, otherOrg.getHouseCommodityValue());
                                              sumArea = priceVersionValueOrgServiceImpl.BigDecimalADD(sumArea, otherOrg.getSaleArea());

                                              buildOrg.setPrice(sumPrice);
                                              buildOrg.setHouseCommodityValue(sumPrice);
                                              buildOrg.setAveragePriceHouse(priceVersionValueOrgServiceImpl.bigDecimalDIV(sumPrice, buildOrg.getSaleArea()));
                                              buildOrg.setRoomSold(sumSoleNum);
                                              buildOrg.setAreaSold(sumSoleArea);
                                              buildOrg.setPriceSold(sumSolePrice);
                                              buildOrg.setRoomPriced(sumNotSaleNum);
                                              buildOrg.setAreaPriced(sumNotSaleArea);
                                              buildOrg.setPricePriced(sumNotSalePrice);

                                              buildInsertList.add(buildOrg);
                                          }else{
                                              //从新计算楼栋的面积、套数、均价、金额(货值)
                                              buildOrg.setSaleArea(roomArea);
                                              buildOrg.setNum(roomNum);
                                              buildOrg.setRoomNum(roomNum);
                                              buildOrg.setAveragePriceHouse(priceVersionValueOrgServiceImpl.bigDecimalDIV(sumPrice, buildOrg.getSaleArea()));
                                              buildOrg.setPrice(sumPrice);
                                              buildOrg.setHouseCommodityValue(sumPrice);
                                              buildOrg.setRoomSold(sumSoleNum);
                                              buildOrg.setAreaSold(sumSoleArea);
                                              buildOrg.setPriceSold(sumSolePrice);
                                              buildOrg.setRoomPriced(sumNotSaleNum);
                                              buildOrg.setAreaPriced(sumNotSaleArea);
                                              buildOrg.setPricePriced(sumNotSalePrice);

                                              buildInsertList.add(buildOrg);
                                          }
                                      }
                                  }

                                  //货值汇总
                                  List<PriceValueOrg> groupInsertList = new ArrayList<PriceValueOrg>();
                                  List<PriceValueOrg> productInsertList = new ArrayList<PriceValueOrg>();
                                  if (CollectionUtils.isNotEmpty(buildInsertList)){
                                      for (PriceValueOrg buildOrg : buildInsertList){
                                          String pid = buildOrg.getPid();
                                          PriceValueOrg valueOrg = productInsertMap.get(pid);
                                          if (valueOrg != null){
                                              priceVersionValueOrgServiceImpl.valueUpSummary(valueOrg, buildOrg);

                                              productInsertMap.put(pid, valueOrg);
                                          }
                                      }

                                      for (Map.Entry<String, PriceValueOrg> pruductMap : productInsertMap.entrySet()){
                                          PriceValueOrg pruductOrg = pruductMap.getValue();
                                          String pid = pruductOrg.getPid();
                                          PriceValueOrg groupOrg = groupInsertMap.get(pid);
                                          if (groupOrg != null){

                                              priceVersionValueOrgServiceImpl.valueUpSummary(groupOrg, pruductOrg);

                                              groupInsertMap.put(pid, groupOrg);
                                          }

                                          productInsertList.add(pruductOrg);
                                      }

                                      for (Map.Entry<String, PriceValueOrg> groupOrgMap : groupInsertMap.entrySet()){
                                          PriceValueOrg groupOrg = groupOrgMap.getValue();
                                          if (groupOrg != null){

                                              priceVersionValueOrgServiceImpl.valueUpSummary(projectOrg, groupOrg);
                                          }

                                          groupInsertList.add(groupOrg);
                                      }
                                  }
                                  List<PriceValueOrg> projectList = Arrays.asList(projectOrg);

                                  //测试保存
                                  try{
                                      priceVersionService.saveInfo(newVersion,projectList,groupInsertList,productInsertList,buildInsertList,hoseSoleList,sortParkList);
                                      transferPriceVersionService.saveInfo(newVersion,projectList,groupInsertList,productInsertList,buildInsertList,hoseSoleList,sortParkList);

                                      transferResult.setStates("1");
                                      transferResult.setRemark("成功");
                                      transferResult.setPriceVersionId(newVersionId);
                                      resultTransferResults.add(transferResult);
                                  }catch (Exception e){
                                      transferResult.setStates("2");
                                      transferResult.setRemark("【异常】保存货值版本出错:"+e.getMessage());
                                      resultTransferResults.add(transferResult);
                                  }
                              }
                          }
                      }else{
                          transferResult.setStates("2");
                          transferResult.setRemark("【金蝶】货值视图不存在对应分期数据");
                          resultTransferResults.add(transferResult);
                      }
                  }
              }catch (MsgException e){
                  transferResult.setStates("2");
                  transferResult.setRemark("【失败】"+e.getMsg());
                  resultTransferResults.add(transferResult);
              }catch (Exception e){
                  transferResult.setStates("2");
                  transferResult.setRemark("【异常】"+e.getMessage());
                  resultTransferResults.add(transferResult);
              }
            }
        }

        if (CollectionUtils.isNotEmpty(resultTransferResults)){
            for (TransferResult transferResult : resultTransferResults){
                transferResultMapper.updateById(transferResult);
            }
        }
    }

    private Map<String, TransferKingdeeValue> jdCollectProduct(String stageCode) {
        Map<String, TransferKingdeeValue> resultMap = new HashMap<String, TransferKingdeeValue>();
        List<TransferKingdeeValue> transferKingdeeValues = transferKingdeeValueService.selectProductValueByStageCode(stageCode);
        if (CollectionUtils.isNotEmpty(transferKingdeeValues)){
            for (TransferKingdeeValue transferKingdeeValue : transferKingdeeValues){
                String productTypeCode = transferKingdeeValue.getProductTypeCode();
                String build = transferKingdeeValue.getBuild();
                String[] ps = build.split("P");
                resultMap.put(ps[1]+"&"+productTypeCode, transferKingdeeValue);
            }
        }

        return resultMap;
    }

    private Map<String, Map<String, TransferKingdeeValue>> jdCollectHouse(String projectCode, List<String> parkingBuildList) {
        Map<String, Map<String, TransferKingdeeValue>> resultMap = new HashMap<String, Map<String, TransferKingdeeValue>>();

        Map<String, TransferKingdeeValue> soldParkingMap = new HashMap<String, TransferKingdeeValue>();
        Map<String, TransferKingdeeValue> houseOrgMap = new HashMap<String, TransferKingdeeValue>();

        List<TransferKingdeeValue> transferKingdeeValues = transferKingdeeValueService.selectValueByProjectCode(projectCode);
        if (CollectionUtils.isNotEmpty(transferKingdeeValues)){
            for (TransferKingdeeValue transferKingdeeValue : transferKingdeeValues){
                String build = transferKingdeeValue.getMdmBuildId();
                if (parkingBuildList.contains(build)){
                    soldParkingMap.put(build, transferKingdeeValue);
                }else{
                    houseOrgMap.put(build, transferKingdeeValue);
                }
            }
        }

        resultMap.put("jdSoldHouse", houseOrgMap);
        resultMap.put("jdSoldParking", soldParkingMap);

        return resultMap;
    }

}
